const app = getApp()
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'
import api from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageBaseURL: app.imageBaseURL,
    picBaseURL:app.picBaseURL,
    index:'1',
    fileList: [],
    imgurl:[],
    submitData:{
      feedbacklist:[],//标签数据
      communityid:'',//青年之家id
      labelid:'',//标签id
      plan:'',//详细方案
      contactways:'',//联系方式
      urlid:[],//图片地址
  
    }  
  },
  //提交
  async submit(){
    if(!this.data.submitData.labelid && !this.data.submitData.plan){
      Toast('请完成必填项')
      return
    }
    let data= this.data.submitData
    this.data.submitData['flag'] = 'activity_community_messageup' 
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      Toast('反馈成功')
      setTimeout(()=>{
        wx.navigateBack({
          delta: 0,
        })
      },3000)
    }else {
      Toast(message)
    }

  },
  //详细方案
  onChangePlan(e){
    this.data.submitData.plan = e.detail;
    this.setData({
      submitData:this.data.submitData
    })
  },
  //联系方式
  onChangePhone(e){
    this.data.submitData.contactways = e.detail;
    this.setData({
      submitData:this.data.submitData
    })
  },
  
  //选择反馈标签
  add(e){
    let ind = e.currentTarget.dataset['index']
    let id = e.currentTarget.dataset['id']

    this.data.feedbacklist.forEach((item,index) =>{
      if(ind == index){
        item.selected = true
      }else{
        item.selected = false
      }
    })
    this.data.submitData.labelid = id;
    this.setData({
      feedbacklist: this.data.feedbacklist,
      submitData:this.data.submitData
    })
  },

  //反馈标签
  async getLabels () {
    let data={}
    data['flag'] = 'activity_community_message'
    
    const {recode, result, message} = await api.send(data)
    if(recode === 'SUCC') {
      result.forEach((item,index) => {
        item.selected = false;
      })
      this.setData({
        feedbacklist:result,
      })
    }else {
      Toast(message)
    }
  },

  //上传图片
  async uploadPic(e){
    let that = this
    wx.uploadFile({
      url: this.data.picBaseURL, //仅为示例，非真实的接口地址
      filePath: e.detail.file.url,
      name: 'files[]',
      formData: {
        'user': 'test'
      },
      success (res){
        const obj = JSON.parse(res.data)
        let fileList = that.data.fileList
        let imgurl = that.data.imgurl
        fileList.push({url:that.data.imageBaseURL+'/' + obj[0].file})
        imgurl.push(obj[0].file)
        that.data.submitData['urlid'] = imgurl;
        console.log(that.data.submitData)
        that.setData({
          submitData:that.data.submitData,
          fileList:fileList,
          imgurl
        })
      }
    })
  },
  delete(e){
    let ind = e.detail.index;
    this.data.fileList.splice(ind,1); 
    this.data.imgurl.splice(ind,1); 
    this.data.submitData['urlid'] = this.data.imgurl;
    this.setData({
      submitData:this.data.submitData,
      fileList:this.data.fileList,
      imgurl:this.data.imgurl
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLabels()
    this.data.submitData.communityid = options.id;
    this.setData({
      communityid:options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})